package com.dome.auth;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.dome.auth.utils.JwtUtils;
import com.dome.util.StringUtils;
import com.dome.util.constans.RedisKeyConstants;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户上下文
 * hz
 */
@Slf4j
public class UserContextHolder {

    private ThreadLocal<Map<String,Object>> threadLocal;

    private ThreadLocal<String> transmittableThreadLocal ; //这里采用TTL的实现

    public UserContextHolder() {
        this.threadLocal=new ThreadLocal<>();
        this.transmittableThreadLocal=new TransmittableThreadLocal<>();
    }

    /**
     * 创建实例
     * @return
     */
    public static UserContextHolder getInstance(){
        return SingletonHolder.sInstance;
    }

    /**
     * 获取灰度发布版本号
     *
     */
    public String getVersion() {
        return transmittableThreadLocal.get();
    }

    /**
     * 静态内部类单列
     * 单列初始化
     */
    private static class SingletonHolder{
        private static final UserContextHolder sInstance=new UserContextHolder();
    }

    public void set(String key,Object value){
        Map<String, Object> stringObjectMap = threadLocal.get();
        if(stringObjectMap==null){
            stringObjectMap=new HashMap<>();
            threadLocal.set(stringObjectMap);
        }
        stringObjectMap.put(key,value);
    }

    public Object get(String key){
        Map<String, Object> stringObjectMap = threadLocal.get();
        if(stringObjectMap==null){
            stringObjectMap=new HashMap<>();
            threadLocal.set(stringObjectMap);
        }
       return stringObjectMap.get(key);
    }

    /**
     * 获取用户的TOKEN
     *
     * @return
     */
    public String getUserToken() {
        String token = getToken();

        if(token!=null) {
            String loginRedisKey = JwtUtils.loginRedisKey(token);
//            log.debug("获取上下文loginRedisKey[{}]",loginRedisKey);
            return loginRedisKey;
        }
        return null;
    }

    /**
     * 用户上下文中放入信息
     *
     */
    public String getToken() {
        Object value = get(RedisKeyConstants.CONTEXT_KEY_USER_TOKEN);
        return StringUtils.getObjectValue(value);
    }


    /**
     * 用户上下文中放入信息
     *
     */
    public void setToken(String token) {
        set(RedisKeyConstants.CONTEXT_KEY_USER_TOKEN,token);
    }

    /**
     * 获取上下文中的用户名
     *
     * @return
     */
    public String getLoginRedisKey() {
        String token = getToken();

        if(token!=null) {
            String loginRedisKey = JwtUtils.loginRedisKey(token);
//            log.debug("获取上下文loginRedisKey[{}]",loginRedisKey);
            return loginRedisKey;
        }
        return null;
    }

    /**
     * 获取登入用户id
     * @return
     */
    public Object getUserId(){
        Object value = get(RedisKeyConstants.CONTEXT_KEY_USER_ID);
        return returnObjectValue(value);
    }

    /**
     * 获取登入用户名
     * @return
     */
    public Object getUserName(){
        Object value = get(RedisKeyConstants.CONTEXT_KEY_USER_NAME);
        return returnObjectValue(value);
    }

    public void setUserName(Object value){
        set(RedisKeyConstants.CONTEXT_KEY_USER_NAME,value);
    }

    public void setUserId(Object value){
       set(RedisKeyConstants.CONTEXT_KEY_USER_ID,value);
    }


    public void setVersion(String version) {
        transmittableThreadLocal.set(version);
    }

    private String returnObjectValue(Object value) {
        return value==null?null:value.toString();
    }

    /**
     * 清空上下文
     */
    public void clear(){
        this.threadLocal.remove();
        this.transmittableThreadLocal.remove();
    }
}
